<!DOCTYPE html>
<uieip type="page" value="login" debug="true" timeout="5000"></uieip>
<uieip type="reg" value="console"></uieip>

<html>

<head>
    <uieip type="file" value="inc" src="/yqwin/easyui/include.txt" cache="true" minute="1440"></uieip>
    <uieip type="file" value="css" src="/yqwin/css/yqwin.min" cache="true"></uieip>
    <uieip type="file" value="js" src="/yqwin/src/yqwin.min" cache="true"></uieip>
    <title>tree edit</title>
    <style>
        body {
            margin: 0px;
        }

        table {
            border-spacing: 0px;
        }
    </style>
</head>

<body>
    <div id="cc" class="easyui-layout" style="width:100%;height:600px;">
        <div data-options="region:'west',title:'角色',split:true" style="width:330px;overflow: hidden;">
            <ul id="tt_roles">
            </ul>
        </div>
        <div data-options="region:'center'">
            <div id="tab_set" data-options="border:false,tabPosition:'bottom'" class="easyui-tabs"
                style="width:100%;height:250px;">
                <div title="人员权限" style="padding:0px;">
                    <div id="tab_setrel" data-options="border:false" class="easyui-tabs"
                        style="width:400px;height:250px;">
                        <div title="岗位" style="padding:1px;">
                            <table id="tb_post" style="width:200px;height: 200px;"></table>
                        </div>
                        <div title="部门" style="padding:1px;">
                            <table id="tb_org" style="width:200px;height: 200px;"></table>
                        </div>
                        <div title="人员" style="padding:1px;">
                            <table id="tb_user" style="width:200px;height: 200px;"></table>
                        </div>
                    </div>
                </div>
                <div title="权限标识" style="padding:0px;">
                    <table>
                        <tr>
                            <td style="vertical-align: top;">
                                <table id="tb_mask_rel" style="width:200px;height: 200px;"></table>
                            </td>
                            <td style="vertical-align: top;">
                                <table style="padding:3px 0 3px 5px;">
                                    <tr>
                                        <td style="padding:0 3px 0 0;">
                                            <lable>系统</lable>
                                        </td>
                                        <td>
                                            <select id="cg_app" style="width: 300px;"></select>
                                        </td>
                                    </tr>
                                </table>

                                <ul id="tt_menu" style="width:400px;height:400px;"></ul>
                                <div id="panel_mask" data-options="border:false" class="easyui-panel" title="页面权限标识"
                                    style="padding:0px;">
                                    <table id="tb_mask" style="width:200px;height:300px;"></table>
                                </div>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <script>
        var dbname = "yqbase";

        var setResizeTime;
        function form_load() {
            onresize();
            $(window).resize(function () {
                onresize();
            });
            var $west = $('#cc').layout("panel", "west");
            $west.panel("options").onResize = function (width, height) {
                if (setResizeTime === undefined) {
                    setResizeTime = setTimeout(function () {
                        onresize();
                    }, 100);
                }
            }

            tabs_init();
            $('#tab_set').tabs("options").onSelect = function (title, index) {
                tabs_select(index);
            };

            tabs_rel_init();
            $('#tab_setrel').tabs("options").onSelect = function (title, index) {
                tabs_rel_reseek(index);
            };

            $('#tt_roles').yqTree("loadData");
        }

        function onresize() {
            var w = window.innerWidth;
            var h = window.innerHeight;
            $('#cc').layout('resize', {
                width: w,
                height: h
            });

            var $west = $('#cc').layout("panel", "west");
            $('#tt_roles').yqTree("resize", {
                width: $west.width(),
                height: $west.height()
            });

            var $center = $('#cc').layout("panel", "center");
            var w = $center.width();
            var h = $center.height();
            $('#tab_set').tabs("resize",
                {
                    width: w,
                    height: h
                });

            w = w;
            h = h - 34;
            $('#tab_setrel').tabs("resize", {
                width: w,
                height: h
            });

            w = w - 2;
            h = h - 36;
            $("#tb_post").yqDataGrid("resize", {
                width: w,
                height: h
            });
            $("#tb_org").yqDataGrid("resize", {
                width: w,
                height: h
            });
            $("#tb_user").yqDataGrid("resize", {
                width: w,
                height: h
            });

            w = $center.width();
            h = $center.height() - 36;
            var rw = w * 0.4;
            if (rw < 350) {
                rw = 350;
            }
            var lw = w - rw - 6;
            $("#tb_mask_rel").yqDataGrid("resize", {
                width: lw,
                height: h
            });

            h = h - 2;
            $("#cg_app").yqComboGrid("resize", (rw - 42) > 250 ? 250 : (rw - 42));
            $("#tt_menu").yqTree("resize", {
                width: rw,
                height: h * 0.6
            });

            $("#panel_mask").panel("resize", {
                width: rw,
                height: h * 0.4
            });
            $("#tb_mask").yqDataGrid("resize", {
                width: rw,
                height: h * 0.4 - 31
            });

            setResizeTime = undefined;
        }

        //---1----------------
        function tabs_init() {
            var panels = $('#tab_set').tabs("tabs");
            for (var i = 0; i < panels.length; i++) {
                panels[i].data("loaddata", false);
            }
        }

        function tabs_reseek() {
            tabs_init();
            var tab = $('#tab_set').tabs('getSelected');
            var index = $('#tab_set').tabs('getTabIndex', tab);
            tabs_select(index);
        }

        var loadmenu = false;
        function tabs_select(index) {
            if (index == 1 && !loadmenu) {
                loadmenu = true;
                $("#cg_app").yqComboGrid("loadData");
                $("#tb_mask").yqDataGrid("setData", { rows: [] });
            }

            var panels = $('#tab_set').tabs("tabs");
            if (!panels[index].data("loaddata")) {
                if (index == 0) {
                    tabs_rel_reseek()
                } else if (index == 1) {
                    $("#tb_mask_rel").yqDataGrid("seek", "");
                }
                panels[index].data("loaddata", true);
            }
        }
        //--1-----------------

        //---2----------------
        function tabs_rel_init() {
            var panels = $('#tab_setrel').tabs("tabs");
            for (var i = 0; i < panels.length; i++) {
                panels[i].data("loaddata", false);
            }
        }

        function tabs_rel_reseek() {
            tabs_rel_init();
            var tab = $('#tab_setrel').tabs('getSelected');
            var index = $('#tab_setrel').tabs('getTabIndex', tab);
            tabs_rel_select(index);
        }

        function tabs_rel_select(index) {
            var panels = $('#tab_setrel').tabs("tabs");
            if (!panels[index].data("loaddata")) {
                if (index == 0) {
                    $("#tb_post").yqDataGrid("seek", "");
                } else if (index == 1) {
                    $("#tb_org").yqDataGrid("seek", "");
                } else if (index == 2) {
                    $("#tb_user").yqDataGrid("seek", "");
                }

                panels[index].data("loaddata", true);
            }
        }
        //--2----------------


        $(function () {
            $.yqDbList({ ajaxUrl: "_db.html" },
                function (data) {
                    if (data.error == 0) {
                        dbname = data.rows[0].name;
                        init_roles();
                    } else {
                        alert(data.info);
                    }
                });
        });

        function init_roles() {
            $('#tt_roles').yqTree({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "sys_roles",
                keyId: "sid",
                pKeyId: "pid",
                pKeyValue: null,

                loadDataType: 2,
                displayMember: "name",
                loadData: false,

                menuType: 3,

                overflowY: true,

                editMode: 2,
                editModeW: 600,
                editModeH: 450,
                editModeNo: -1,

                beforeBindingTitle: function (titles, callback) {
                    //console.log("beforeBindingTitle", titles);
                    callback(titles);
                },
                afterBindingTitle: function () {
                    init_post();
                },

                beforeSetdata: function (data, callback) {
                    //console.log("beforeSetdata", data);
                    callback(data)
                },
                afterSetdata: function (data) {
                    //console.log("afterSetdata", data);
                },
                formatter: function (node) {
                    var text = node.text;
                    if (text == null) {
                        text = "xxx";
                    }
                    return text;
                },
                onClick: function (node) {     //节点点击
                    var roles_type = 0;
                    if (!(node === null)) {
                        roles_type = node.attributes["roles_type"];
                    }

                    $("#tb_post").yqDataGrid("clear");
                    $("#tb_org").yqDataGrid("clear");
                    $("#tb_user").yqDataGrid("clear");
                    $("#tb_mask_rel").yqDataGrid("clear");
                    if (node === null || roles_type != 1) {
                        $("#tb_post").yqDataGrid("setBindingValue", null);
                        $("#tb_org").yqDataGrid("setBindingValue", null);
                        $("#tb_user").yqDataGrid("setBindingValue", null);
                        $("#tb_mask_rel").yqDataGrid("setBindingValue", null);
                    } else {
                        $("#tb_post").yqDataGrid("setBindingValue", node.id);
                        $("#tb_org").yqDataGrid("setBindingValue", node.id);
                        $("#tb_user").yqDataGrid("setBindingValue", node.id);
                        $("#tb_mask_rel").yqDataGrid("setBindingValue", node.id);

                        tabs_reseek();
                    }
                },
            });
        }

        function init_post() {
            $("#tb_post").yqDataGrid({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "sys_roles_post_rel",
                keyId: "sid",
                bindingKeyId: "roles_id",
                bindingValue: null,

                addWhere: "",
                order: "queue",

                editMode: 3,
                editModeW: 600,
                editModeH: 450,
                editModeNo: 1,
                loadData: false,
                isPage: false,

                isEdit: true,
                copy: true,
                add: true,
                upp: true,
                del: true,
                isdel: false,
                isdelEdit: false,
                move: true,

                editLoadData: true,

                isSimple: false,
                isSeek: false,

                afterBindingTitle: function () {
                    init_org();
                },
            });
        }

        function init_org() {
            $("#tb_org").yqDataGrid({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "sys_roles_org_rel",
                keyId: "sid",
                bindingKeyId: "roles_id",
                bindingValue: null,

                addWhere: "",
                order: "queue",

                editMode: 3,
                editModeW: 600,
                editModeH: 450,
                editModeNo: 1,
                loadData: false,
                isPage: false,

                isEdit: true,
                copy: true,
                add: true,
                upp: true,
                del: true,
                isdel: false,
                isdelEdit: false,
                move: true,

                editLoadData: true,

                isSimple: false,
                isSeek: false,

                afterBindingTitle: function () {
                    init_user();
                },
            });
        }

        function init_user() {
            $("#tb_user").yqDataGrid({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "sys_roles_user_rel",
                keyId: "sid",
                bindingKeyId: "roles_id",
                bindingValue: null,

                addWhere: "",
                order: "queue",

                editMode: 3,
                editModeW: 600,
                editModeH: 450,
                editModeNo: 1,
                loadData: false,
                isPage: false,

                isEdit: true,
                copy: true,
                add: true,
                upp: true,
                del: true,
                isdel: false,
                isdelEdit: false,
                move: true,

                editLoadData: true,

                isSimple: false,
                isSeek: false,

                afterBindingTitle: function () {
                    init_mask_rel();
                },
            });
        }

        function init_mask_rel() {
            $("#tb_mask_rel").yqDataGrid({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "sys_roles_mark_rel",
                keyId: "sid",
                bindingKeyId: "roles_id",
                bindingValue: null,

                addWhere: "",
                order: "queue",

                editMode: 3,
                editModeW: 600,
                editModeH: 450,
                editModeNo: 1,
                loadData: false,
                isPage: false,

                isEdit: true,
                copy: false,
                add: false,
                upp: false,
                del: true,
                isdel: false,
                isdelEdit: false,
                move: true,

                editLoadData: true,

                isSimple: false,
                isSeek: false,

                afterBindingTitle: function () {
                    init_app();
                },
                afterDataAction: function (rows) {
                    $("#tb_mask").yqDataGrid("uppActionState");
                },
            });
        }

        function init_app() {
            $("#cg_app").yqComboGrid({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "sys_menu_app",
                keyId: "sid",
                displayMember: "name",

                titles: [],
                loadData: false,
                isEdit: false,
                isSeek: false,

                isPage: false,

                editMode: 2,
                editModeW: 600,
                editModeH: 450,
                editModeNo: -1,

                afterBindingTitle: function () {
                    init_menu();
                },

                onChange: function (newValue, newRow, oldValue, oldRow) {
                    var v = $(this).yqComboGrid("getValue");
                    if (newValue == null) {
                        $('#tt_menu').yqTree("setBindingValue", null);
                        $('#tt_menu').yqTree("clear");
                    }
                    else {
                        $('#tt_menu').yqTree("setBindingValue", newValue);
                        $('#tt_menu').yqTree("loadData");
                    }
                },
            });
        }

        function init_menu() {
            $('#tt_menu').yqTree({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "sys_menu",
                keyId: "sid",
                bindingKeyId: "app_id",
                bindingValue: null,
                pKeyId: "pid",
                pKeyValue: null,

                loadDataType: 2,
                displayMember: "name",
                loadData: false,

                overflowY: true,

                isEdit: false,

                editMenus: [{
                    id: "link",
                    text: "菜单设置",
                    iconCls: "icon2-link",
                }],
                editLinks_r: [{
                    id: "link_r",
                    text: "菜单设置",
                    iconCls: "icon2-link",
                }],
                uppActionState: function (s_node, callback) {
                    var menu_type = 0;
                    if (s_node != null) {
                        menu_type = s_node.attributes["menu_type"];
                        if (menu_type === undefined || menu_type == null) {
                            menu_type = 0;
                        }
                    }

                    if (menu_type == 1 || menu_type == 2) {
                        $('#tt_menu').yqTree("btnEnable", "link");
                        $('#tt_menu').yqTree("btnEnable", "link_r");
                    } else {
                        $('#tt_menu').yqTree("btnDisable", "link");
                        $('#tt_menu').yqTree("btnDisable", "link_r");
                    }

                    callback(true);
                },
                btnAction: function (action,params, callback) {
                    if (action == "link" || action == "link_r") {
                        var node = $(this).yqTree("getSelected");
                        if (node != null) {
                            treeDblClick(node);
                        }
                        callback(false);
                    } else {
                        callback(true);
                    }
                },

                beforeBindingTitle: function (titles, callback) {
                    //console.log("beforeBindingTitle", titles);
                    callback(titles);
                },
                afterBindingTitle: function () {
                    init_mark();
                },

                beforeSetdata: function (data, callback) {
                    //console.log("beforeSetdata", data);
                    callback(data)
                },
                afterSetdata: function (data) {
                    //console.log("afterSetdata", data);
                },
                formatter: function (node) {
                    //menu_type 0 文件夹,1 菜单界面,2 弹出界面
                    var menu_type = node.attributes["menu_type"];
                    if (menu_type === undefined || menu_type == null) {
                        menu_type = 0;
                    }
                    var text = node.text;
                    if (node.text == null) {
                        text = node.attributes["describe"];
                    }

                    return text;
                },
                onClick: function (node) {     //节点点击
                    var menu_type = 0;
                    if (!(node === null)) {
                        menu_type = node.attributes["menu_type"];
                        if (menu_type === undefined || menu_type == null) {
                            menu_type = 0;
                        }
                    }

                    if (menu_type == 1) {
                        $("#tb_mask").yqDataGrid("setBindingValue", node.id);
                        $("#tb_mask").yqDataGrid("seek", "");
                    } else {
                        $("#tb_mask").yqDataGrid("clear");
                        $("#tb_mask").yqDataGrid("setBindingValue", null);
                    }
                },
            });

            function treeDblClick(node) {
                var menu_type = node.attributes["menu_type"];
                if (menu_type === undefined || menu_type == null) {
                    menu_type = 0;
                }
                if (!(menu_type == 1 || menu_type == 2)) {
                    return;
                }

                window.parent.$MidWin.yqFormMdi("editwin", {
                    id: node.attributes["sid"],
                    title: (node.text == null ? node.attributes["name"] : node.text),
                    openmore: false,
                    content: "",
                    url: "/adp/menuedit.html?dbname=" + dbname + "&menu_id=" + node.id,
                    closable: true,
                });
            }
        }

        function init_mark() {
            $("#tb_mask").yqDataGrid({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "sys_menu_mark",
                keyId: "sid",
                bindingKeyId: "menu_id",
                bindingValue: null,
                pKeyId: null,
                pKeyValue: null,

                tempno: 1,

                addWhere: "",
                order: "queue",

                editMode: 3,
                loadData: false,
                isPage: false,

                isEdit: false,


                isSimple: false,
                isSeek: false,

                editLinks: [{
                    id: "link",
                    text: "关联权限标识[单条]",
                    iconCls: "icon2-left",
                }, {
                    id: "links",
                    text: "关联权限标识[全部]",
                    iconCls: "icon2-lefts",
                }],
                popMenuWidth: 170,     //右键菜单第一层宽度
                uppActionState: function (s_index, s_row, callback) {
                    var bindingValue = $("#tb_mask_rel").yqDataGrid("options").bindingValue;

                    if (s_row === null || bindingValue === null) {
                        $("#tb_mask").yqDataGrid("btnDisable", "link");
                        $("#tb_mask").yqDataGrid("btnDisable", "links");
                        callback(false);
                    } else {
                        var link_rows = $("#tb_mask_rel").yqDataGrid("getRows");
                        var mask_rows = $("#tb_mask").yqDataGrid("getRows");

                        var hasrow = false;
                        for (var i = 0; i < link_rows.length; i++) {
                            if (link_rows[i]["mark_id"] == s_row["sid"]) {
                                hasrow = true;
                                break;
                            }
                        }
                        if (hasrow) {
                            $("#tb_mask").yqDataGrid("btnDisable", "link");
                        } else {
                            $("#tb_mask").yqDataGrid("btnEnable", "link");
                        }

                        var hasrows = true;
                        var hasrowsnum = 0;
                        for (var j = 0; j < mask_rows.length; j++) {
                            for (var i = 0; i < link_rows.length; i++) {
                                if (link_rows[i]["mark_id"] == mask_rows[j]["sid"]) {
                                    hasrowsnum++;
                                    break;
                                }
                            }
                        }
                        if (hasrowsnum < mask_rows.length) {
                            hasrows = false;
                        }

                        if (hasrows) {
                            $("#tb_mask").yqDataGrid("btnDisable", "links");
                        } else {
                            $("#tb_mask").yqDataGrid("btnEnable", "links");
                        }

                        callback(true);
                    }
                },
                btnAction: function (action, params,callback) {
                    var bindingValue = $("#tb_mask_rel").yqDataGrid("options").bindingValue;
                    if (action == "link") {
                        var row = $("#tb_mask").yqDataGrid("getSelected");
                        var s_row = {
                            roles_id: bindingValue,
                            mark_id: row["sid"]
                        };
                        $("#tb_mask_rel").yqDataGrid("dataAction", [{ action: "add", row: s_row }]);
                    } else if (action == "links") {
                        var link_rows = $("#tb_mask_rel").yqDataGrid("getRows");
                        var mask_rows = $("#tb_mask").yqDataGrid("getRows");
                        var a_rows = [];
                        for (var j = 0; j < mask_rows.length; j++) {
                            var hasrow = false;
                            for (var i = 0; i < link_rows.length; i++) {
                                if (link_rows[i]["mark_id"] == mask_rows[j]["sid"]) {
                                    hasrow = true;
                                    break;
                                }
                            }
                            if (!hasrow) {
                                a_rows.push({
                                    action: "add", row: {
                                        roles_id: bindingValue,
                                        mark_id: mask_rows[j]["sid"]
                                    }
                                });
                            }
                        }

                        if (a_rows.length > 0) {
                            $("#tb_mask_rel").yqDataGrid("dataAction", a_rows);
                        }
                    }
                    callback(false);
                },
                beforeBindingTitle: function (columns, callback) {
                    callback(columns);
                }, //标题绑定前
                afterBindingTitle: function () {
                    form_load();
                }, //标题绑定后
            });
        }

    </script>
</body>

</html>